// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TaskFactoryExtensions.StartNew.cs" company="JDML Consulting Ltd.">
//   Copyright 2011 Photon Project - http://photon.codeplex.com
// </copyright>
// <summary>
//   Defines the TaskFactoryExtensions type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Photon.Threading
{
    using System;

    public static partial class TaskFactoryExtensions
   {
      private static ITask<T> Start<T>(ITask<T> task, TaskCallBack<T> callBack)
      {
         task.Start(callBack);
         return task;
      }

      #region ITask<T>

      public static ITask<TReturn> StartNew<TReturn>(
         this TaskFactory instance, BeginAsyncMethod begin, EndAsyncMethod<TReturn> end)
      {
         return instance.StartNew(begin, end, null);
      }

      public static ITask<TReturn> StartNew<TReturn>(this TaskFactory instance, BeginAsyncMethod begin, 
                                                     EndAsyncMethod<TReturn> end,  TaskCallBack<TReturn> callback)
      {
         return Start(instance.FromAsync(begin, end), callback);
      }
   
      public static ITask<TReturn> StartNew<TArg1, TReturn>(this TaskFactory instance, BeginAsyncMethod<TArg1> begin, EndAsyncMethod<TReturn> end, TArg1 arg1)
      {
         return instance.StartNew(begin, end, arg1, null);
      }

      public static ITask<TReturn> StartNew<TArg1, TReturn>(this TaskFactory instance, BeginAsyncMethod<TArg1> begin, EndAsyncMethod<TReturn> end, TArg1 arg1, TaskCallBack<TReturn> callback)
      {
         return Start(instance.FromAsync(begin, end, arg1), callback);
      }
      
      public static ITask StartNew<TArg1, TArg2, TReturn>(this TaskFactory instance, BeginAsyncMethod<TArg1, TArg2> begin, EndAsyncMethod<TReturn> end, TArg1 arg1, TArg2 arg2)
      {
         return instance.StartNew(begin, end, arg1, arg2, null);
      }

      public static ITask StartNew<TArg1, TArg2, TReturn>(this TaskFactory instance, BeginAsyncMethod<TArg1, TArg2> begin, EndAsyncMethod<TReturn> end, TArg1 arg1, TArg2 arg2, TaskCallBack<TReturn> callback)
      {
         return Start(instance.FromAsync(begin, end, arg1, arg2), callback);
      }

      #endregion

      private static ITask Start(ITask task, TaskCallBack callBack)
      {
         task.Start(callBack);
         return task;
      }

      #region ITask
      
      public static ITask StartNew(this TaskFactory instance, Action action)
      {
         return Start(instance.From(action), null);
      }

      public static ITask StartNew(this TaskFactory instance, Action action, TaskCallBack callBack)
      {
         return Start(instance.From(action), callBack);
      }

      public static ITask StartNew(this TaskFactory instance, BeginAsyncMethod begin, EndAsyncMethod end)
      {
         return instance.StartNew(begin, end, null);
      }

      public static ITask StartNew(this TaskFactory instance, BeginAsyncMethod begin, EndAsyncMethod end,
                                   TaskCallBack callBack)
      {
         return Start(instance.FromAsync(begin, end), callBack);
      }

      public static ITask StartNew<TArg1>(this TaskFactory instance, TArg1 arg1, BeginAsyncMethod<TArg1> begin, 
                                          EndAsyncMethod end)
      {
         return instance.StartNew(arg1, begin, end, null);
      }

      public static ITask StartNew<TArg1>(this TaskFactory instance, TArg1 arg1, BeginAsyncMethod<TArg1> begin, EndAsyncMethod end,
                                          TaskCallBack callBack)
      {
         return Start(instance.FromAsync(begin, end, arg1), callBack);
      }

      public static ITask StartNew<TArg1, TArg2>(this TaskFactory instance, TArg1 arg1, TArg2 arg2, 
                                                 BeginAsyncMethod<TArg1, TArg2> begin, EndAsyncMethod end)
      {
         return instance.StartNew(arg1, arg2, begin, end, null);
      }

      public static ITask StartNew<TArg1, TArg2>(this TaskFactory instance, TArg1 arg1, TArg2 arg2, 
                                                 BeginAsyncMethod<TArg1, TArg2> begin, EndAsyncMethod end, TaskCallBack callBack)
      {
         return Start(instance.FromAsync(begin, end, arg1, arg2), callBack);
      }

      #endregion
   }
}